package org.example.demo;

import lombok.RequiredArgsConstructor;
import org.example.demo.service.SpringAiService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/spring-ai")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class SpringAiController {

    private final SpringAiService springAiService;

    /**
     * 简单聊天接口
     */
    @GetMapping("/chat")
    public Map<String, Object> chat(@RequestParam("query") String query) {
        try {
            String response = springAiService.getSimpleChatResponse(query);
            return Map.of(
                "success", true,
                "data", response,
                "message", "聊天成功"
            );
        } catch (Exception e) {
            return Map.of(
                "success", false,
                "data", "",
                "message", "聊天失败: " + e.getMessage()
            );
        }
    }

    /**
     * 模板聊天接口
     */
    @PostMapping("/chat/template")
    public Map<String, Object> chatWithTemplate(@RequestBody Map<String, String> request) {
        try {
            String topic = request.get("topic");
            String style = request.get("style");
            String response = springAiService.getChatResponseWithTemplate(topic, style);
            return Map.of(
                "success", true,
                "data", response,
                "message", "模板聊天成功"
            );
        } catch (Exception e) {
            return Map.of(
                "success", false,
                "data", "",
                "message", "模板聊天失败: " + e.getMessage()
            );
        }
    }

    /**
     * 代码生成接口
     */
    @PostMapping("/generate-code")
    public Map<String, Object> generateCode(@RequestBody Map<String, String> request) {
        try {
            String language = request.get("language");
            String description = request.get("description");
            String response = springAiService.generateCode(language, description);
            return Map.of(
                "success", true,
                "data", response,
                "message", "代码生成成功"
            );
        } catch (Exception e) {
            return Map.of(
                "success", false,
                "data", "",
                "message", "代码生成失败: " + e.getMessage()
            );
        }
    }

    /**
     * 文本摘要接口
     */
    @PostMapping("/summarize")
    public Map<String, Object> summarizeText(@RequestBody Map<String, Object> request) {
        try {
            String text = (String) request.get("text");
            Integer maxLength = (Integer) request.getOrDefault("maxLength", 200);
            String response = springAiService.summarizeText(text, maxLength);
            return Map.of(
                "success", true,
                "data", response,
                "message", "文本摘要成功"
            );
        } catch (Exception e) {
            return Map.of(
                "success", false,
                "data", "",
                "message", "文本摘要失败: " + e.getMessage()
            );
        }
    }

    /**
     * 健康检查接口
     */
    @GetMapping("/health")
    public Map<String, Object> health() {
        return Map.of(
            "success", true,
            "data", "Spring AI Demo is running",
            "message", "服务正常运行",
            "timestamp", System.currentTimeMillis()
        );
    }
}